<?php
namespace addons\baiduwebtongji\library;
use addons\baiduwebtongji\library\ReportService;
/**
 * 百度网站统计api
 */
class TongjiApi {
	protected $domain;
	protected $siteId;
	/**
	* 参数赋值、登录获取siteId
	*/
	public function __construct($config)
	{
		// define('LOGIN_URL', 'https://api.baidu.com/sem/common/HolmesLoginService');
		define('API_URL', 'https://api.baidu.com/json/tongji/v1/ReportService');
		define('USERNAME', $config['username']);
		define('PASSWORD', $config['password']);
		define('TOKEN', $config['token']);
		define('UUID', $config['uuid']);
		define('ACCOUNT_TYPE', $config['account_type']); 
		$this->domain=$config['domain'];
		$this->siteId=$config['siteId'];	
    }
    /**
	* 获取数据
	* @param string $method 查询方法
	* @param string $mertics 查询参数
	* @param string $order 排序
	*/
	private function getData($param)
	{
		$reportService = new ReportService(API_URL, USERNAME,TOKEN,PASSWORD);
		if(!$this->siteId){
			$ret = $reportService->getSiteList();
			$siteList = $ret['body']['data'][0]['list'];
			if($siteList){
				if (count($siteList) > 0) {
					if($this->domain){
						foreach ($siteList as $key => $value) {
							if($this->domain==$value["domain"]){
								$this->siteId=$value["site_id"];
								break;
							}else{
								continue;
							}
						}
						if(!$this->siteId){
							$this->siteId = $siteList[0]['site_id'];
						}
					}else{
						$this->siteId = $siteList[0]['site_id'];
					}
				}else{
					return false;
				}
			}else{
				return false;
			}
		}
		$param['site_id']=$this->siteId;
	    $res = $reportService->getData($param);
	    return json_decode($res['raw'],true);
	}
	/**
	 * 获取网站今日流量与昨日对比
	 */
	public function getOutline()
	{
		$param=[
			'method'		=>	'overview/getOutline',
		];
		return $this->getData($param);
	}
	/**
	* 获取今日趋势分析数据
	*/
	public function trendTimeA()
	{
		$param=[
			'method'	=>	'trend/time/a',
			'metrics'	=>	'pv_count,visitor_count',//,ip_count,bounce_ratio,avg_visit_time
			'start_date'=>	date("Ymd"),
			'end_date'	=>	date("Ymd"), 
			'order'		=>	'simple_date_title,asc'
		];
		$res=$this->getData($param);
		return $res;
	}

	/**
	 * [getTimeTrendRpt 网站概况]
	 * @return [type] [description]
	 */
	public function getTimeTrendRpt($start_date,$end_date){
		$param=[
			'method'		=>	'overview/getTimeTrendRpt',
			'metrics'		=>	'pv_count,visitor_count',
			'start_date'	=>	$start_date,
			'end_date'		=>	$end_date, 
		];
		return $this->getData($param);
	}

	/**
	* 获取访问地域分布数据
	*/
	public function getDistrictRpt($start_date,$end_date)
	{
		$param=[
			'method'=>'overview/getDistrictRpt',
			'metrics'=>'pv_count,visitor_count',
			'start_date'=>	$start_date,
			'end_date'	=>	$end_date, 
		];
		return $this->getData($param);
	}

	/**
	* 获取访问搜索词、来源网站、入口界面、受访页面、新老访客数据
	*/
	public function getCommonTrackRpt($start_date,$end_date)
	{
		$param=[
			'method'=>'overview/getCommonTrackRpt',
			'metrics'=>'pv_count',
			'start_date'=>	$start_date,
			'end_date'	=>	$end_date, 
		];
		return $this->getData($param);
	}

	/**
	 * 实时访客
	 * @param  [int] $page        [页码]
	 * @param  [int] $max_results [每页数量]
	 * @return [array] [description]
	 */
	public function trendLatestA($page,$max_results){
		$start_index=($page-1)*$max_results;
		$param=[
			'method'		=>	'trend/latest/a',
			'metrics'		=>	'start_time,area,source,access_page,keyword,searchword,ip,visit_time,visit_pages',
			'max_results'	=> 	$max_results,
			'start_index'	=>	$start_index
		];
		return $this->getData($param);
	}

	/**
	 * [sourceAll 全部来源]
	 * @param  [int] $page        [页码]
	 * @param  [int] $max_results [每页数量]
	 * @param  [date] $start_date  [开始时间]
	 * @param  [date] $end_date    [结束时间]
	 * @return [array]              [description]
	 */
	public function sourceAll($page,$max_results,$start_date,$end_date){
		$start_index=($page-1)*$max_results;
		$param=[
			'method'		=>	'source/all/a',
			'metrics'		=>	'pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time',
			'viewType'		=>	'site',
			'max_results'	=> 	$max_results,
			'start_index'	=>	$start_index,
			'start_date'	=>	$start_date,
			'end_date'		=>	$end_date, 
		];
		return $this->getData($param);
	}

	/**
	 * [sourceSearchword 搜索词]
	 * @param  [int] $page        [页码]
	 * @param  [int] $max_results [每页数量]
	 * @param  [date] $start_date  [开始时间]
	 * @param  [date] $end_date    [结束时间]
	 * @return [array]              [description]
	 */
	public function sourceSearchword($page,$max_results,$start_date,$end_date){
		$start_index=($page-1)*$max_results;
		$param=[
			'method'		=>	'source/searchword/a',
			'metrics'		=>	'pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time',
			'viewType'		=>	'site',
			'max_results'	=> 	$max_results,
			'start_index'	=>	$start_index,
			'start_date'	=>	$start_date,
			'end_date'		=>	$end_date, 
		];
		return $this->getData($param);
	}

	/**
	 * [visitToppage 受访页面]
	 * @param  [int] $page        [页码]
	 * @param  [int] $max_results [每页数量]
	 * @param  [date] $start_date  [开始时间]
	 * @param  [date] $end_date    [结束时间]
	 * @return [array]              [description]
	 */
	public function visitToppage($page,$max_results,$start_date,$end_date){
		$start_index=($page-1)*$max_results;
		$param=[
			'method'		=>	'visit/toppage/a',
			'metrics'		=>	'pv_count,visitor_count,ip_count,visit1_count,outward_count,exit_count,exit_ratio,average_stay_time',
			'max_results'	=> 	$max_results,
			'start_index'	=>	$start_index,
			'start_date'	=>	$start_date,
			'end_date'		=>	$end_date, 
		];
		return $this->getData($param);
	}

	/**
	 * [visitDistrict 地域分布(按省)]
	 * @param  [int] $page        [页码]
	 * @param  [int] $max_results [每页数量]
	 * @param  [date] $start_date  [开始时间]
	 * @param  [date] $end_date    [结束时间]
	 * @return [array]              [description]
	 */
	public function visitDistrict($param){
		$param['method']='visit/district/a';
		return $this->getData($param);
	}

	/**
	 * [visitDistrictTop 地域分布(按城市)]
	 * @param  [type] $start_date [开始时间]
	 * @param  [type] $end_date   [结束时间]
	 * @param  [type] $area       [省份]
	 * @return [type]             [description]
	 */
	public function visitDistrictTop($start_date,$end_date,$area){
		$param=[
			'method'		=>	'visit/district/top',
			'metrics'		=>	'pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time',
			'start_date'	=>	$start_date,
			'end_date'		=>	$end_date,
			'viewType'		=>	'city',
			'area'			=>	$area
		];
		return $this->getData($param);
	}

	/**
	 * [visitWorld 地域分布(按国家)]
	 * @param  [int] $page        [页码]
	 * @param  [int] $max_results [每页数量]
	 * @param  [date] $start_date  [开始时间]
	 * @param  [date] $end_date    [结束时间]
	 * @return [array]              [description]
	 */
	public function visitWorld($start_date,$end_date){
		$param=[
			'method'		=>	'visit/world/a',
			'metrics'		=>	'pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time',
			'start_date'	=>	$start_date,
			'end_date'		=>	$end_date, 
		];
		return $this->getData($param);
	}
	public function rankGetData($start_date,$end_date){
		$param=[
			'method'		=>	'opt/searchwordrank/getData',
			'metrics'		=>	'',
			'start_date'	=>	$start_date,
			'end_date'		=>	$end_date, 
			'viewType'		=>	'searchword_rank'
		];
		return $this->getData($param);
	}
	public function rankGetWords(){
		$param=[
			'method'		=>	'opt/searchwordrank/getWords',
		];
		return $this->getData($param);
	}
	public function rankSetWords($words){
		$param=[
			'method'	=>	'opt/searchwordrank/setWords',
			'words'		=>	$words
		];
		return $this->getData($param);
	}
	public function reportSend($param){
		$param['method']	=	'home/reportsend/add';
		return $this->getData($param);
	}
}